package com.sskj.one.view;

import android.app.Activity;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.hjq.toast.ToastUtils;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.lib.Constans;
import com.sskj.one.R;
import com.sskj.one.util.ArithUtil;

import java.util.List;

import dagger.internal.InstanceFactory;

public class BottomDialog {

    public static void showShopCheck(Activity activity, List<String> datas, OnSure onSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.account_bottom_dialog, null, false);
        RecyclerView recyclerView = inflate.findViewById(R.id.recyclerView);
        TextView tv_cancel = inflate.findViewById(R.id.tv_cancel);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(activity)
                .setPaintColor(ContextCompat.getColor(activity, R.color.divide))
                .setPaintWidth((int) RudenessScreenHelper.dp2px(activity, 1))
                .setFirstDraw(false)
                .setLastDraw(false));
        SlimAdapter.create().register(R.layout.item_bottom_dialog, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector iViewInjector, List list) {
                iViewInjector.text(R.id.tv_bottom_dialog, data)
                        .clicked(R.id.tv_bottom_dialog, new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                onSure.onSure(data);
                                bottomSheetDialog.dismiss();
                            }
                        });
            }
        }).attachTo(recyclerView).updateData(datas);
        ClickUtil.click(300, tv_cancel, () -> {
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void bankerTransfer(Activity activity, String type, String all, String fee, OnTransferSure onTransferSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.dialog_banker_transfer, null, false);
        TextView tv_money_type = inflate.findViewById(R.id.tv_money_type);
        TextView tv_title = inflate.findViewById(R.id.tv_title);
        EditText et_money = inflate.findViewById(R.id.et_money);
        TextView tv_all = inflate.findViewById(R.id.tv_all);
        TextView tv_all_money = inflate.findViewById(R.id.tv_all_money);
        EditText et_pwd = inflate.findViewById(R.id.et_pwd);
        LinearLayout ll_fee = inflate.findViewById(R.id.ll_fee);
        TextView tv_fee = inflate.findViewById(R.id.tv_fee);
        Button btn_confirm = inflate.findViewById(R.id.btn_confirm);
        MoneyValueFilter moneyValueFilter = new MoneyValueFilter();
        moneyValueFilter.setDigits(5);
        et_money.setFilters(new InputFilter[]{moneyValueFilter});
        if (type.equals(Constans.TRANSFER_IN)) {
            tv_title.setText("从交易账户转入股池");
            et_money.setHint("请输入转入金额");
            tv_money_type.setText("交易账户余额：");
            et_money.setEnabled(true);
            btn_confirm.setText("确定转入");
            ll_fee.setVisibility(View.GONE);
        } else if (type.equals(Constans.TRANSFER_OUT)) {
            tv_title.setText("从股池转出至交易账户");
            et_money.setHint("请输入转出金额");
            tv_money_type.setText("股池账户余额：");
            et_money.setText(all);
            et_money.setEnabled(false);
            btn_confirm.setText("确定转出");
            ll_fee.setVisibility(View.GONE);
        }
        tv_all_money.setText("$" + all);
        tv_fee.setText("$" + NumberUtil.keep(String.valueOf(ArithUtil.mul(Double.valueOf(all), Double.valueOf(fee))), 5));
        ClickUtil.click(300, tv_all, () -> {
            et_money.setText(all);
        });
        ClickUtil.click(300, btn_confirm, () -> {
            if (TextUtils.isEmpty(et_money.getText().toString().trim())) {
                ToastUtils.show(et_money.getHint());
                return;
            }
            if (Double.valueOf(et_money.getText().toString().trim()) == 0) {
                if (type.equals(Constans.TRANSFER_IN)) {
                    ToastUtils.show("转入金额必须大于0");
                } else if (type.equals(Constans.TRANSFER_OUT)) {
                    ToastUtils.show("转出金额必须大于0");
                }
                return;
            }
            if (TextUtils.isEmpty(et_pwd.getText().toString().trim())) {
                ToastUtils.show(et_pwd.getHint());
                return;
            }
            onTransferSure.onSure(et_money.getText().toString().trim(), et_pwd.getText().toString().trim());
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void jiehui(Activity activity, String type, OnGouhuiSure onGouhuiSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.dialog_jiehui, null, false);
        TextView tv_title = inflate.findViewById(R.id.tv_title);
        EditText et_name = inflate.findViewById(R.id.et_name);
        EditText et_account = inflate.findViewById(R.id.et_account);
        Button btn_confirm = inflate.findViewById(R.id.btn_confirm);
        if (type.equals(Constans.GOUHUI_WECHAT)) {
            tv_title.setText("结汇到微信");
            et_account.setHint("请输入微信账号");
        } else if (type.equals(Constans.GOU_HUI_ALIPAY)) {
            tv_title.setText("结汇到支付宝");
            et_account.setHint("请输入支付宝账号");
        }
        ClickUtil.click(300, btn_confirm, () -> {
            if (TextUtils.isEmpty(et_name.getText().toString().trim())) {
                ToastUtils.show(et_name.getHint().toString().trim());
                return;
            }
            if (TextUtils.isEmpty(et_account.getText().toString().trim())) {
                ToastUtils.show(et_account.getHint().toString().trim());
                return;
            }
            onGouhuiSure.onSure(et_name.getText().toString().trim(), et_account.getText().toString().trim());
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public static void jiehuiBank(Activity activity, OnGouhuiSure onGouhuiSure) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.dialog_jiehui_bank, null, false);
        EditText et_name = inflate.findViewById(R.id.et_name);
        EditText et_bank = inflate.findViewById(R.id.et_bank);
        EditText et_branch = inflate.findViewById(R.id.et_branch);
        EditText et_bank_card = inflate.findViewById(R.id.et_bank_card);
        Button btn_confirm = inflate.findViewById(R.id.btn_confirm);
        ClickUtil.click(300, btn_confirm, () -> {
            if (TextUtils.isEmpty(et_name.getText().toString().trim())) {
                ToastUtils.show(et_name.getHint().toString().trim());
                return;
            }
            if (TextUtils.isEmpty(et_bank.getText().toString().trim())) {
                ToastUtils.show(et_bank.getHint().toString().trim());
                return;
            }
            if (TextUtils.isEmpty(et_branch.getText().toString().trim())) {
                ToastUtils.show(et_branch.getHint().toString().trim());
                return;
            }
            if (TextUtils.isEmpty(et_bank_card.getText().toString().trim())) {
                ToastUtils.show(et_bank_card.getHint().toString().trim());
                return;
            }
            if (et_bank_card.getText().toString().trim().length() < 15) {
                ToastUtils.show("请输入正确的银行卡号");
                return;
            }
            onGouhuiSure.onSure(et_name.getText().toString().trim()
                    , et_bank.getText().toString().trim()
                    , et_branch.getText().toString().trim()
                    , et_bank_card.getText().toString().trim());
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }


    public static void transaction(Activity activity, String type) {
        BottomSheetDialog bottomSheetDialog = new BottomSheetDialog(activity);
        View inflate = activity.getLayoutInflater().inflate(R.layout.dialog_transaction, null, false);
        TextView tv_type = inflate.findViewById(R.id.tv_type);
        TextView tv_type_c = inflate.findViewById(R.id.tv_type_c);
        TextView tv_buy = inflate.findViewById(R.id.tv_buy);
        TextView tv_odds = inflate.findViewById(R.id.tv_odds);
        TextView tv_hope_income = inflate.findViewById(R.id.tv_hope_income);
        TextView tv_end_time = inflate.findViewById(R.id.tv_end_time);
        TextView tv_now_price = inflate.findViewById(R.id.tv_now_price);
        MyRadioGroup radio = inflate.findViewById(R.id.radio);
        Button btn_confirm = inflate.findViewById(R.id.btn_confirm);
        if (type.equals(Constans.BUY_UP)) {
            tv_type.setText("（买涨）");
            tv_type_c.setText("买涨");
            tv_type.setTextColor(ContextCompat.getColor(activity, R.color.color_red));
            tv_type_c.setTextColor(ContextCompat.getColor(activity, R.color.color_red));
        } else if (type.equals(Constans.BUY_DOWN)) {
            tv_type.setText("（买跌）");
            tv_type_c.setText("买跌");
            tv_type.setTextColor(ContextCompat.getColor(activity, R.color.theme));
            tv_type_c.setTextColor(ContextCompat.getColor(activity, R.color.theme));
        }
        radio.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_ten:
                    tv_buy.setText("$ 10");
                    break;
                case R.id.rb_twenty:
                    tv_buy.setText("$ 20");
                    break;
                case R.id.rb_thirty:
                    tv_buy.setText("$ 30");
                    break;
                case R.id.rb_forty:
                    tv_buy.setText("$ 50");
                    break;
                case R.id.rb_hundred:
                    tv_buy.setText("$ 100");
                    break;
                case R.id.rb_five_hundred:
                    tv_buy.setText("$ 500");
                    break;
                case R.id.rb_thousand:
                    tv_buy.setText("$ 1000");
                    break;
                case R.id.rb_two_thousand:
                    tv_buy.setText("$ 5000");
                    break;
            }
        });
        ClickUtil.click(300, btn_confirm, () -> {
            bottomSheetDialog.dismiss();
        });
        bottomSheetDialog.setContentView(inflate);
        bottomSheetDialog.setCancelable(true);
        bottomSheetDialog.setCanceledOnTouchOutside(true);
        bottomSheetDialog.show();
    }

    public interface OnGouhuiSure {
        void onSure(String... strings);
    }


    public interface OnSure {
        void onSure(String tv);
    }

    public interface OnTransferSure {
        void onSure(String money, String pwd);
    }
}
